package com.lanou.moving.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.adapter.SearchAlbumAdapter;
import com.lanou.moving.entity.SearchAlbumRoot;
import com.lanou.moving.utils.MyConfig;
import com.lanou.moving.utils.NoScrollListView;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class AlbumFragment extends Fragment implements AdapterView.OnItemClickListener {

    private List<SearchAlbumRoot.DataEntity> datas;
    private String url;
    private VolleySingleton volleySingleton;
    private SearchAlbumAdapter adapter;
    private NoScrollListView noScrollListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.search_album_page, null);
        noScrollListView = (NoScrollListView) view.findViewById(R.id.album_list);
        noScrollListView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initData();
        return view;

    }

    private void initData() {
        //接收数据替换q
        String q = MyConfig.getSharePreStr(getActivity(), "Searchcontent", "url");
        Log.i("hehe", "---" + q);
        url = "http://api.dongting.com/misc/search/album?uid=867831024341380&f=f62&" +
                "app=ttpod&hid=1280524740415150&rom=Xiaomi%252Fcancro_wc_lte%252Fcancro%253A4.4.4%252FKTU84P%252FV6.4.3.0.KXDCNCB%253Auser%252Frelease-keys&alf=alf702008&cpu=msm8974&resolution=1080x1920&net=2&size=50&v=v8.2.0.2015091720&utdid=VeKf29%2BKwt4DANT3MxDH4mCl&s=s200&ram=2932124+kB&page=1&" +
                "q=" + q + "&splus=4.4.4%252F19&cpu_model=Qualcomm+MSM8974PRO-AC";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SearchAlbumRoot albumRoot = gson.fromJson(response, SearchAlbumRoot.class);
                datas = albumRoot.getData();
                adapter = new SearchAlbumAdapter(getActivity(), datas);
                noScrollListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchAlbumRoot.DataEntity entity = (SearchAlbumRoot.DataEntity) parent.getItemAtPosition(position);
        AlbumDetailFragment fragment = new AlbumDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("albumId", Long.valueOf(entity.getAlbumId()));
        fragment.setArguments(bundle);
        getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).addToBackStack(null).commit();

    }
}
